//
//  CZStatusTableViewCell.swift
//  weibo
//
//  Created by Apple on 16/3/15.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SnapKit

/*
    首页上的cell有三大块视图
    1.原创微博视图
    2.转发微博视图
    3.微博底部的转发评论赞的toolbar
*/

let StatusCellMargin: CGFloat = 10
let nameLabelFontSize: CGFloat = 14
let timeLabelFontSize: CGFloat = 10
let contentLabelFontSize: CGFloat = 15

class CZStatusTableViewCell: UITableViewCell {
    //  保存上次statusToolBar的顶部约束
    var statusToolBarTopConstraint: Constraint?
    var statusViewModel: CZStatusViewModel? {
        didSet{
            originalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            //  卸载约束
            statusToolBarTopConstraint?.uninstall()
            //  更新约束
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.statusToolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                retweetView.hidden = true
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.statusToolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })

            }
            
        }
    }
    
    // MARK:    懒加载控件
    private lazy var originalView: CZStatusOriginalView = {
        let originView = CZStatusOriginalView()
        originView.backgroundColor = UIColor.whiteColor()
        return originView
    }()
    //  底部toolBar
    private lazy var statusToolBar: CZStatusToolBar = CZStatusToolBar()
    
    //  转发微博视图
    private lazy var retweetView: CZStatusRetweetView = CZStatusRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        //  cell的样式改成subtitle，能显示出来detailLabel内容
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
        setupUI()
        //  去掉选择颜色
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {

        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //  添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(retweetView)
        
        //  设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
//            make.height.equalTo(45)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            self.statusToolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        
        
        
        //  非常关键的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    
    
    

}
